package com.hpin.mlxteacher.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hpin.mlxteacher.R;
import com.hpin.mlxteacher.base.BaseActivity;
import com.hpin.mlxteacher.utils.CommonUtils;
import com.hpin.mlxteacher.utils.LogUtil;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * 忘记密码页面
 * Created by Aaron on 15/8/3.
 */
public class ForgetPswActivity extends BaseActivity {

    private EditText et_photo;
    private EditText et_verify_code;
    private TextView tv_get_verify_code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_forget_psw);

        initWidget();
    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        tv_get_verify_code = (TextView) findViewById(R.id.tv_get_verify_code);

        tv_title.setText(getResources().getString(R.string.forget_psw_title));

        et_photo = (EditText) findViewById(R.id.et_photo);
        et_verify_code = (EditText) findViewById(R.id.et_verify_code);

        Button btn_commit = (Button) findViewById(R.id.btn_commit);

        iv_left.setOnClickListener(this);
        tv_get_verify_code.setOnClickListener(this);
        btn_commit.setOnClickListener(this);

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
            case R.id.tv_get_verify_code: //获取验证码

                if (CommonUtils.isNull(et_photo.getText().toString().trim())) {
                    showToast("请输入手机号码");
                    return;
                }

                //倒计时
                Countdown();

                //获取验证码
                getSms();

                break;
            case R.id.btn_commit: //提交

                commuit();

                break;

        }

    }

    /**
     * 提交
     */
    private void commuit() {

        if (CommonUtils.isNull(et_photo.getText().toString().trim())) {
            showToast("请输入手机号码");
            return;
        }

        if (CommonUtils.isNull(et_verify_code.getText().toString().trim())) {
            showToast("请输入验证码");
            return;
        }

        //提交验证
        SMSSDK.submitVerificationCode("86", et_photo.getText().toString().trim(), et_verify_code.getText().toString().trim());

    }

    private void getSms() {

        EventHandler eventHandler = new EventHandler() {
            public void afterEvent(int event, int result, Object data) {
//                show.dismiss();
                if (result == SMSSDK.RESULT_COMPLETE) {
                    //回调完成

                    if (event == SMSSDK.EVENT_SUBMIT_VERIFICATION_CODE) {
                        //提交验证码验证成功
                        LogUtil.d("sms", "==========提交验证码成功 =========");
                        Intent intent = new Intent(ForgetPswActivity.this, ResetPswActivity.class);
                        intent.putExtra("phone",et_photo.getText().toString().trim());
                        startActivity(intent);

                    }else if (event == SMSSDK.EVENT_GET_VERIFICATION_CODE){
                        //获取验证码成功
                        LogUtil.d("sms", "==========获取验证码成功 =========");
                        // 开始计时
//                        runOnUiThread(new Runnable() {
//                            public void run() {
////                                btn_code.startTickWork();
//                            }
//                        });

                    }else if (event ==SMSSDK.EVENT_GET_SUPPORTED_COUNTRIES){
                        //返回支持发送验证码的国家列表
                    }
                }else{
                    //验证失败
                    showToast("验证码错误");
                }
            }
        };
        // 注册回调监听接口
        SMSSDK.registerEventHandler(eventHandler);
        //提交发送短信的请求
        SMSSDK.getVerificationCode("86", et_photo.getText().toString().trim());
        ready = true;

    }

    boolean ready;
    protected void onDestroy() {
        if (ready) {
            // 销毁回调监听接口
            SMSSDK.unregisterAllEventHandler();
        }
        super.onDestroy();
    }


    /**
     * 倒计时
     */
    private void Countdown() {

        tv_get_verify_code.setClickable(false);
        tv_get_verify_code.setEnabled(false);
        if (executor==null) {
            executor = Executors.newSingleThreadExecutor();
        }

        tv_get_verify_code.setText(currentTime + " 秒");
        for (int i = 0; i < 60; i++) {

            executor.execute(new Runnable() {

                @Override
                public void run() {

                    SystemClock.sleep(1000);

                    changeTime();
                }
            });

        }

    }

    int currentTime = 60;
    private ExecutorService executor;

    /**
     * 改变界面时间
     */
    private void changeTime() {
        runOnUiThread(new Runnable() {
            public void run() {

                if (currentTime==1) {
                    currentTime = 60;
                    tv_get_verify_code.setText(getResources().getString(R.string.get_verify_code));
                    tv_get_verify_code.setClickable(true);
                    tv_get_verify_code.setEnabled(true);

                } else{
                    tv_get_verify_code.setText(--currentTime + " 秒");
                }
            }
        });
    }
}
